package com.example.viewpageapplication.adapter;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.viewpageapplication.R;
import com.example.viewpageapplication.entity.BaseT;

import java.util.ArrayList;
import java.util.List;

public class ViewPageVadapter extends PagerAdapter {

    private List<BaseT> mbast;
    private Context mcontext;

    private List<View> mviewList = new ArrayList<>();
    public ViewPageVadapter(Context context,List<BaseT> mbast) {
        this.mbast = mbast;
        this.mcontext = context;

        for (int i = 0; i <mbast.size(); i++) {
            View one_item = View.inflate(mcontext, R.layout.one_item,null);
            ImageFilterView ifv = one_item.findViewById(R.id.ifv_item);
            RadioGroup ra_group = one_item.findViewById(R.id.rgp_start);
            Button btn_start = one_item.findViewById(R.id.btn_start);
            ifv.setImageResource(BaseT.Images[i]);
            //设置循环动态加载按钮
            for (int j = 0; j < mbast.size(); j++) {

                RadioButton radioButton = new RadioButton(mcontext);
                radioButton.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                ));
                radioButton.setPadding(10,10,10,10);
                ra_group.addView(radioButton);
            }
            //设置当前按钮高亮显示
            ((RadioButton) ra_group.getChildAt(i)).setChecked(true);
            //最后页面显示按钮
            if(i == mbast.size() - 1){
                btn_start.setVisibility(View.VISIBLE);
                btn_start.setText("欢迎来到精彩世界");
            }
            mviewList.add(one_item);
        }
    }

    @Override
    public int getCount() {
        return mviewList.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //实例化指定位置的页面，并将其添加到容器内
        container.addView(mviewList.get(position),0);
        return mviewList.get(position);

    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(mviewList.get(position));
    }
}
